package com.westar.core.dao;

import com.westar.base.model.PasswdMd5;
import com.westar.base.model.UserDepAreaRelate;
import com.westar.base.model.UserInfo;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户操作的dao层
 */
@Repository
public class UserInfoDao extends BaseDao {
    /**
     * 设置用户的正式密码
     * @param account 账号
     * @param password 密码
     */
    public void updateUserInfo(String account, String password,String md5) {
        StringBuffer sql = new StringBuffer();
        List<Object> args = new ArrayList<>(2);

        sql.append( "UPDATE userInfo SET passwd=?,md5=? WHERE account=?" );
        args.add( password );
        args.add( md5 );
        args.add( account );
        //执行更新操作
        int code = this.excuteSql( sql.toString(),args.toArray() );
        //没有更新的则实现添加操作
        if(0 == code){
            UserInfo userInfo = new UserInfo();
            userInfo.setAccount( account );
            userInfo.setPasswd( password );
            userInfo.setMd5(md5);
            this.add(userInfo);
        }
    }

    /**
     * 修改md5库
     * @param password
     * @param md5
     */
    public void updatePasswdMd5(String password,String md5) {
        StringBuffer sql = new StringBuffer();
        List<Object> args = new ArrayList<>(2);

        sql.append( "UPDATE passwdMd5 SET passwd=?  WHERE md5=?" );
        args.add( password );
        args.add( md5 );
        //执行更新操作
        int code = this.excuteSql( sql.toString(),args.toArray() );
        //没有更新的则实现添加操作
        if(0 == code){
            PasswdMd5 userInfo = new PasswdMd5();
            userInfo.setPasswd( password );
            userInfo.setMd5(md5);
            this.add(userInfo);
        }
    }

    /**
     * 根据用户账号获取用户信息
     * @param account 当前操作人员
     * @return
     */
    public UserInfo getUserInfoByAccount(String account) {
        StringBuffer sql = new StringBuffer();
        List<Object> args = new ArrayList<>(1);
        sql.append( "\n SELECT a.*,area.areacode from userInfo a" );
        sql.append( "\n left join userdeparearelate b on a.account=b.account" );
        sql.append( "\n left join area on b.areaid=area.areaid" );
        sql.append( "\n WHERE 1=1" );
        this.addSqlWhere(account,sql,args," AND a.account=?");
        return (UserInfo) this.objectQuery( sql.toString(),args.toArray(),UserInfo.class );
    }

    /**
     * 查询对应名称的办理人员
     * @param users
     * @return
     */
    public List<UserInfo> listAllOperator(String[] users) {
        StringBuffer sql = new StringBuffer();
        List<Object> args = new ArrayList<>(1);
        sql.append( "\n SELECT a.* " );
        sql.append( "\n from userInfo a" );
        sql.append( "\n inner join userDepAreaRelate b on a.account=b.account" );
        sql.append( "\n WHERE 1=1" );
        this.addSqlWhereIn(users,sql,args," and b.name in ?");
        return this.listQuery(sql.toString(),args.toArray(),UserInfo.class);
    }

    /**
     * 通过账号查找对应的md5然后找到密码
     * @param account
     * @return
     */
    public UserInfo getUserInfoByAccountWithMd5(String account){
        StringBuffer sql = new StringBuffer();
        List<Object> args = new ArrayList<>(1);
        sql.append( "\n SELECT b.account,c.passwd passwd,c.md5" );
        sql.append( "\n from userDepAreaRelate b" );
        sql.append( "\n inner join passwdMd5 c on b.md5Passwd=c.md5" );
        sql.append( "\n WHERE 1=1" );
        this.addSqlWhere(account,sql,args," and b.account = ?");
        return (UserInfo) this.objectQuery(sql.toString(),args.toArray(),UserInfo.class);
    }

    /**
     * 修改同步表的用户密码关系表
     * @param account
     * @param md5
     */
    public void updateUserDepAreaRelate(String account, String md5) {

        StringBuffer sql = new StringBuffer();
        List<Object> args = new ArrayList<>(2);

        sql.append( "UPDATE userDepAreaRelate SET md5Passwd=? WHERE account=?" );
        args.add( md5 );
        args.add( account );
        //执行更新操作
        this.excuteSql( sql.toString(),args.toArray() );
    }

    /**
     * 查询需要进行MD5转换的用户
     * @return
     */
    public List<UserInfo> listToMd5User() {
        StringBuffer sql = new StringBuffer();
        List<Object> args = new ArrayList<>(2);
        sql.append("\n select b.account,b.md5passwd md5,c.passwd  ");
        sql.append("\n from userinfo a");
        sql.append("\n inner join userdeparearelate b on a.ACCOUNT=b.ACCOUNT");
        sql.append("\n left join passwdMd5 c on b.md5passwd=c.md5");
        sql.append("\n where a.md5<>b.md5passwd");
        return this.listQuery(sql.toString(),args.toArray(),UserInfo.class);

    }

    /**
     * 查询所有没有进行md5的用户
     * @return
     */
    public List<UserInfo> listAllUserWithoutMd5() {
        StringBuffer sql = new StringBuffer();
        List<Object> args = new ArrayList<>(2);
        sql.append("\n select a.* ");
        sql.append("\n from userinfo a");
        sql.append("\n where 1=1 and a.md5 is null");
        return this.listQuery(sql.toString(),args.toArray(),UserInfo.class);
    }

    /**
     * 查询需要解密的用户
     * @return
     */
    public List<UserInfo> listUserForPasswd() {
        StringBuffer sql = new StringBuffer();
        List<Object> args = new ArrayList<>(2);
        sql.append("\n select a.account,a.md5");
        sql.append("\n from userinfo a");
        sql.append("\n where passwd='-'");
        return this.listQuery(sql.toString(),args.toArray(),UserInfo.class);
    }
    /**
     * 查询需要修改密码的用户
     * @return
     */
    public List<UserInfo> listUserForChangePwd() {
        StringBuffer sql = new StringBuffer();
        List<Object> args = new ArrayList<>(2);
        sql.append("\n select a.account,a.md5");
        sql.append("\n from userinfo a");
        sql.append("\n where passwd<>'-'");
        return this.listQuery(sql.toString(),args.toArray(),UserInfo.class);
    }

    /**
     * 查询需要解密的用户
     * @return
     */
    public UserDepAreaRelate getUserDepAreaRelateByAccount(String account) {
        StringBuffer sql = new StringBuffer();
        List<Object> args = new ArrayList<>(2);
        sql.append("\n select a.*");
        sql.append("\n from userDepAreaRelate a");
        sql.append("\n where 1=1 ");
        this.addSqlWhere(account,sql,args," \n and a.account=?");
        return (UserDepAreaRelate) this.objectQuery(sql.toString(),args.toArray(),UserDepAreaRelate.class);
    }

    /**
     * 查询需要解密的用户
     * @return
     */
    public List<UserDepAreaRelate> listAllUserDepAreaRelate(String[] users) {
        StringBuffer sql = new StringBuffer();
        List<Object> args = new ArrayList<>(2);
        sql.append("\n select a.*");
        sql.append("\n from userDepAreaRelate a");
        sql.append("\n where 1=1 ");
        this.addSqlWhereIn(users,sql,args," and a.name in ?");
        return this.listQuery(sql.toString(),args.toArray(),UserDepAreaRelate.class);
    }
}
